from django.db import models
from rbac.models import UserInfo as RbacUserInfo

# Create your models here.


class School(models.Model):
    """
    校区表：
    如：北京昌平校区，上海黄埔校区，深圳南山校区
    """
    title = models.CharField(max_length=128,verbose_name='校区名称')
    address=models.CharField(max_length=256,verbose_name="学校地址")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural=verbose_name='学校'




class Department(models.Model):
    """
    部门表：
    """
    title = models.CharField(max_length=128,verbose_name='部门名称')


    def __str__(self):
        return self.title

    class Meta:
        verbose_name='部门'
        verbose_name_plural ='部门'

#
# class Student(models.Model):
#     """
#     学生表
#     """
#     name=models.CharField(max_length=128,verbose_name='姓名')
#
#     def __str__(self):
#         return self.name
#
#     class Meta:
#         verbose_name_plural=verbose_name='学生'


class UserInfo(RbacUserInfo):
    """
    员工表，用户表，继承着rbac user info,已有属性，name password email roles
    """
    # password = models.C(max_length=256, verbose_name='密码')
    nick_name=models.CharField(verbose_name='昵称',max_length=128,null=True,blank=True)
    phone=models.CharField(verbose_name='手机号',max_length=20,null=True,blank=True)
    gender_choices=(
        (1,'男'),
        (2,'女'),
    )
    gender=models.IntegerField(verbose_name='性别',choices=gender_choices,default=1)
    depart=models.ForeignKey('Department',null=True,blank=True,on_delete=models.SET_NULL,verbose_name='部门')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural=verbose_name='用户'


class Course(models.Model):
    """
    课程表
    """
    name=models.CharField(max_length=128,verbose_name='课程名称')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = verbose_name = '课程'

class ClassList(models.Model):
    """
    班级表
    """
    school=models.ForeignKey(verbose_name='校区',null=True,blank=True,on_delete=models.SET_NULL,to='School')
    course=models.ForeignKey(verbose_name='课程',null=True,blank=True,on_delete=models.SET_NULL,to='Course')
    semester=models.PositiveIntegerField(verbose_name='班级(期)')
    price=models.PositiveIntegerField(verbose_name='学费')
    start_date=models.DateField(verbose_name='开班日期',null=True,blank=True)
    graduate_date=models.DateField(verbose_name='结业日期',null=True,blank=True)
    #仅显示教职部的老师
    tutor=models.ForeignKey(verbose_name='班主任',to='UserInfo',related_name='classes',null=True,blank=True,
                            on_delete=models.SET_NULL,limit_choices_to={"depart__title":'教职部'})
    teachers=models.ManyToManyField(verbose_name='任课老师',to='UserInfo',related_name='teach_classes',blank=True,
                                    limit_choices_to={"depart__title__in":['Python教学部','Linux教学部']})
    memo = models.TextField(verbose_name='说明',null=True, blank=True)

    def __str__(self):
        return "{} {}期".format(self.course,self.semester)

    class Meta:
        verbose_name='班级'
        verbose_name_plural=verbose_name


class Customer(models.Model):
    """
    客户表
    """
    name=models.CharField(verbose_name='姓名',max_length=128)
    qq=models.CharField(verbose_name='联系方式',max_length=64,unique=True,help_text='QQ号/微信/手机号')
    status_choices=(
        (1,'已报名'),
        (2,'未报名')
    )
    status=models.IntegerField(verbose_name='状态',choices=status_choices,default=2)
    gender_choices=(
        (1,'男'),
        (2,'女'),
        (3,'未知')
    )
    gender=models.IntegerField(verbose_name='性别',choices=gender_choices,default=3)
    source_choices=(
        (1,'QQ群'),
        (2, '内部转介绍'),
        (3, '官方网站'),
        (4, '百度推广'),
        (5, '360推广'),
        (6, '搜狗推广'),
        (7, '腾讯课堂'),
        (8, '广点通'),
        (9, '高校宣讲'),
        (10, '渠道代理'),
        (11, '51cto'),
        (12, '智汇推'),
        (13, '网盟'),
        (14, 'DSP'),
        (15, 'SEO'),
        (16, '其它'),
    )
    source=models.SmallIntegerField(verbose_name='客户来源',choices=source_choices,default=1)
    referral_form=models.ForeignKey('self',verbose_name='转介绍学员',null=True,blank=True,
                                    help_text='若此客户是自内部学员转介绍的，请在此处选择内部学员姓名',
                                    related_name='internal_referral',
                                    on_delete=models.SET_NULL)
    course=models.ManyToManyField('Course',verbose_name='咨询课程',blank=True)
    #有客户顾问的为私人客户
    consultant=models.ForeignKey(verbose_name='课程顾问',to='UserInfo',related_name='consultant',
                                 null=True,blank=True,on_delete=models.SET_NULL,
                                 limit_choices_to={"depart__title":'销售部'})
    education_choices=(
        (1, '重点大学'),
        (2, '普通本科'),
        (3, '独立院校'),
        (4, '民办本科'),
        (5, '大专'),
        (6, '民办专科'),
        (7, '高中'),
        (8, '其他'),
    )
    education=models.IntegerField(verbose_name='学历',choices=education_choices,null=True,blank=True)
    graduation_school=models.CharField(verbose_name='毕业院校',max_length=64,null=True,blank=True)
    major=models.CharField(verbose_name='所学专业',max_length=128,null=True,blank=True)
    experience_choices=(
        (1, '在校生'),
        (2, '应届毕业'),
        (3, '半年以内'),
        (4, '半年至一年'),
        (5, '一年至三年'),
        (6, '三年至五年'),
        (7, '五年以上')
    )
    experience=models.IntegerField(verbose_name='工作经验',blank=True,null=True,choices=experience_choices)
    work_status_choices=(
        (1, '在职'),
        (2, '无业'),
    )
    work_status=models.IntegerField(verbose_name='工作状态',blank=True,null=True,choices=work_status_choices)
    company=models.CharField(verbose_name='目前就职公司',max_length=64,blank=True,null=True)
    salary=models.CharField(verbose_name='当前薪资',max_length=64,blank=True,null=True)
    date=models.DateField(verbose_name='咨询日期',auto_now_add=True)
    last_consult_date=models.DateField(verbose_name='最后跟进日期',auto_now=True)

    def __str__(self):
        return "姓名：{}，联系方式：{}".format(self.name,self.qq)

    class Meta:
        verbose_name_plural = verbose_name='客户'


class ConsultRecord(models.Model):
    '''
    客户的跟进记录
    '''
    customer=models.ForeignKey('Customer',verbose_name='所咨询的客户',on_delete=models.CASCADE)
    consultant=models.ForeignKey('UserInfo',verbose_name='跟踪人',null=True,blank=True,on_delete=models.SET_NULL,
                                 limit_choices_to={"depart__title":'销售部'})
    date=models.DateField(verbose_name='跟进日期',auto_now_add=True)
    note=models.TextField(verbose_name='跟进内容')

    def __str__(self):
        return "{}对{}的跟进".format(self.consultant,self.customer.name)

    class Meta:
        verbose_name_plural = verbose_name='客户记录跟进'


class PaymentRecord(models.Model):
    '''
    缴费申请
    '''
    customer=models.ForeignKey(to='Customer',verbose_name='客户',on_delete=models.CASCADE)
    consultant=models.ForeignKey(to='UserInfo',verbose_name='课程顾问',null=True,blank=True,on_delete=models.SET_NULL,
                                 related_name='consultants')
    pay_type_choices=(
        (1,'报名费'),
        (2,'学费'),
        (3,'退学'),
        (4,'其他')
    )
    pay_type=models.IntegerField(verbose_name='费用类型',choices=pay_type_choices,default=1)
    paid_fee=models.PositiveIntegerField(verbose_name='金额',default=0)
    class_list=models.ForeignKey(to='ClassList',verbose_name='申请班级',null=True,blank=True,on_delete=models.SET_NULL)
    apply_date=models.DateTimeField(verbose_name='申请日期',auto_now_add=True)
    confirm_status_choices=(
        (1, '申请中'),
        (2, '已确认'),
        (3, '已驳回'),
    )
    confirm_status=models.IntegerField(verbose_name='确认状态',choices=confirm_status_choices,default=1)
    confirm_date=models.DateTimeField(verbose_name='确认日期',null=True,blank=True)
    confirm_user=models.ForeignKey(to='UserInfo',verbose_name='审批人',null=True,blank=True,on_delete=models.SET_NULL,
                                   related_name='confirms')
    note=models.TextField(verbose_name='备注',blank=True,null=True)

    def __str__(self):
        return "{}缴费{}元".format(self.customer.name,self.paid_fee)

    class Meta:
        verbose_name_plural = verbose_name='缴费记录'


class Student(models.Model):
    '''
    学生表，已缴费学员
    '''
    customer=models.OneToOneField('Customer',verbose_name='客户信息',null=True,blank=True,on_delete=models.SET_NULL)
    qq=models.CharField(verbose_name='QQ号',max_length=32,null=True,blank=True)
    mobile=models.CharField(verbose_name='手机号',max_length=32,null=True,blank=True)
    emergency_contract=models.CharField(verbose_name='紧急联系人电话',max_length=32,null=True,blank=True)
    class_list=models.ManyToManyField('ClassList',verbose_name='已报班级',blank=True)
    student_status_choices=(
        (1, '申请中'),
        (2, '在读'),
        (3, '毕业'),
        (4,'退学'),
    )
    score=models.IntegerField('积分',default=100)
    student_status=models.IntegerField('学员状态',choices=student_status_choices,default=1)
    memo=models.TextField('备注',max_length=255,blank=True,null=True)

    def __str__(self):
        return self.customer.name

    class Meta:
        verbose_name_plural = verbose_name = '学生'


class ScoreRecord(models.Model):
    """
    积分记录
    """
    student=models.ForeignKey(to='Student',verbose_name='学生',on_delete=models.SET_NULL,null=True,blank=True)
    content=models.TextField('理由')
    score=models.IntegerField(verbose_name='分值',help_text='违纪扣分写负值，表现良好加分写正值')
    user=models.ForeignKey(to='UserInfo',verbose_name='执行人',on_delete=models.SET_NULL,
                           blank=True,null=True)
    def __str__(self):
        return '的积分'.format(self.student.customer.name)

    class Meta:
        verbose_name_plural = verbose_name = '学员积分'

class CourseRecord(models.Model):
    """
    上课记录
    """
    class_object=models.ForeignKey('ClassList',verbose_name='班级',on_delete=models.CASCADE)
    day_num=models.IntegerField('节次')
    teacher=models.ForeignKey('UserInfo',verbose_name='讲师',null=True,blank=True,on_delete=models.SET_NULL)
    date=models.DateField('上课日期',auto_now_add=True)

    def __str__(self):
        return "{} day{}".format(self.class_object,self.day_num)

    class Meta:
        verbose_name_plural = verbose_name = '上课记录'

class StudyRecord(models.Model):
    """
        考勤记录
    """
    course_record=models.ForeignKey("CourseRecord",verbose_name='第几天课程',null=True,blank=True,on_delete=models.SET_NULL)
    student=models.ForeignKey('Student',verbose_name='学员',on_delete=models.CASCADE)
    record_choices=(
        ('checked','已签到'),
        ('vacate','请假'),
        ('late','迟到'),
        ('noshow','缺勤'),
        ('leave_early','早退')
    )
    record=models.CharField('上课记录',choices=record_choices,default='checked',max_length=64)

    def __str__(self):
        return "{} {}".format(self.student.customer.name,self.course_record)

    class Meta:
        verbose_name_plural = verbose_name = '考勤记录'

